package com.aisoback.manager;

import com.aisoback.common.Code;
import com.aisoback.exception.BusinessException;
import com.aisoback.model.Picture;
import com.aisoback.model.Posts;
import com.aisoback.model.Request.QueryUserRequest;
import com.aisoback.model.Request.SearchRequest;
import com.aisoback.model.User;
import com.aisoback.model.enums.SearchTypeEnum;
import com.aisoback.model.vo.QueryPostsVO;
import com.aisoback.model.vo.SearchVo;
import com.aisoback.service.PictureService;
import com.aisoback.service.PostsService;
import com.aisoback.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class SearchUtils {
    @Resource
    private UserService userService;
    @Resource
    private PostsService postsService;
    @Resource
    private PictureService pictureService;

    public SearchVo SearchAll(SearchRequest searchRequest) throws IOException {
        String type = searchRequest.getType();
        String searchText = searchRequest.getSearchText();
        SearchVo searchVo = new SearchVo();
        SearchTypeEnum value = SearchTypeEnum.getEnumByValue(type);
        if (value == null) {
            try {
                //查询用户
                QueryUserRequest queryUserRequest = new QueryUserRequest();
                queryUserRequest.setUsername(searchText);
                Page<User> userPage = userService.getUserList(queryUserRequest, 1, 10);
                //查询帖子
                QueryPostsVO queryPostsVO = new QueryPostsVO();
                queryPostsVO.setTitle(searchText);
                Page<Posts> postPage = postsService.getPostPage(queryPostsVO, 1, 10);
                //查询图片
                Page<Picture> picturePage = pictureService.getPictureList(searchText, 1, 10);
                //将获取到的数据封装到SearchVo对象中
                searchVo.setUserList(userPage.getRecords());
                searchVo.setPostList(postPage.getRecords());
                searchVo.setPictureList(picturePage.getRecords());
                return searchVo;
            } catch (Exception e) {
                throw new BusinessException(Code.SYSTEM_ERROR, e.getMessage());
            }
        } else {
            switch (value) {
                case USER:
                    //查询用户
                    QueryUserRequest queryUserRequest = new QueryUserRequest();
                    queryUserRequest.setUsername(searchText);
                    Page<User> userPage = userService.getUserList(queryUserRequest, 1, 10);
                    searchVo.setUserList(userPage.getRecords());
                    break;
                case POST:
                    //查询帖子
                    QueryPostsVO queryPostsVO = new QueryPostsVO();
                    queryPostsVO.setTitle(searchText);
                    Page<Posts> postPage = postsService.getPostPage(queryPostsVO, 1, 10);
                    searchVo.setPostList(postPage.getRecords());
                    break;
                case PICTURE:
                    Page<Picture> picturePage = pictureService.getPictureList(searchText, 1, 10);
                    searchVo.setPictureList(picturePage.getRecords());
                    break;
                default:
                    throw new BusinessException(Code.PARAMS_ERROR, "参数错误");
            }
            return searchVo;
        }
    }
}
